package com.guanglan.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.guanglan.domain.SysRole;
import com.guanglan.service.SysRoleService;
import com.guanglan.utils.PageUtil;

/**
* @ClassName: RoleController  
* @Description: TODO(用户管理类)  
* @author GuangLan good
* @date 2017年9月10日  
 */
@RequestMapping("/role")
@Controller
public class RoleController {
	
	Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private SysRoleService sysRoleService;
	
	@RequestMapping("/roleIndex")
	public String roleIndex(@RequestParam(value="nowPage",required=false,defaultValue="1") Integer nowPage,
			Model model) {
		int start = PageUtil.getStart(nowPage);  //获取到的开始查询索引
		int pageSize = PageUtil.pageSize; //获取页面容量
		Map<String,Object> params = new HashMap<>();
		params.put("start", start);
		params.put("pageSize", pageSize);
		List<SysRole> roles = sysRoleService.getRoles(params);
		int count = sysRoleService.getRolesCount(params);
		int countPage = PageUtil.countPage(count);
		String html = PageUtil.getPanationStringHtml(countPage, nowPage);
		model.addAttribute("html",html);
		model.addAttribute("roles",roles);
		return "role/roleIndex";
	}
	
	@RequestMapping("/addOrUpdateRole")
	public String addOrUpdateRole(@RequestParam(value="id",required=false,defaultValue="1") String id,
			@RequestParam(value="roleName",required=false,defaultValue="1") String roleName) {
		
		SysRole sr = new SysRole();
		sr.setRoleName(roleName);
		if(StringUtils.trimToNull(id) != null) {
			sr.setId(Long.valueOf(id));
			int count = sysRoleService.updateByPrimaryKeySelective(sr);
			log.info("更新角色信息{}",count > 0 ? "成功":"失败");
		}else {
			int count = sysRoleService.insertSelective(sr);
			log.info("新增插入角色信息{}",count > 0 ? "成功":"失败");
		}
		
		return "redirect:roleIndex";
	}
	
	@RequestMapping("/delRole")
	public String delRole(@RequestParam(value="id",required=false,defaultValue="1") long id) {
		int count = sysRoleService.deleteByPrimaryKey(id);
		log.info("del用户信息{}",count > 0 ? "成功":"失败");
		return "redirect:roleIndex";
	}

}
